﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Runtime.InteropServices;

namespace SkyDriveEx.IconOverlay
{
    public static class ShellComRegister
    {


        public static void RegisterShellIconOverlayIdentifier(Type type)
        {
            using (RegistryKey approved = Registry.LocalMachine.CreateSubKey(
              @"Software\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved",
              RegistryKeyPermissionCheck.ReadWriteSubTree))
            {
                approved.SetValue(type.GUID.ToString("B").ToUpper(), " _" + type.Name);
            }

            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(
              @"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifiers",
              RegistryKeyPermissionCheck.ReadWriteSubTree,
              System.Security.AccessControl.RegistryRights.FullControl))
            {
                using (RegistryKey sioi = key.CreateSubKey(" _" + type.Name))
                {
                    sioi.SetValue(String.Empty, type.GUID.ToString("B").ToUpper());
                }
            }

            ShellMethods.SHChangeNotify(Enums.SHCNE.ASSOCCHANGED, 0, IntPtr.Zero, IntPtr.Zero);

            ShellMethods.SHLoadNonloadedIconOverlayIdentifiers();
        }

        public static void UnregisterShellIconOverlayIdentifier(Type type)
        {
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(
              @"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifiers",
              RegistryKeyPermissionCheck.ReadWriteSubTree,
              System.Security.AccessControl.RegistryRights.FullControl))
            {
                key.DeleteSubKey(" _" + type.Name, false);
            }

            using (RegistryKey approved = Registry.LocalMachine.OpenSubKey(
              @"Software\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved",
              RegistryKeyPermissionCheck.ReadWriteSubTree,
              System.Security.AccessControl.RegistryRights.FullControl))
            {
                approved.DeleteValue(" _" + type.GUID.ToString("B").ToUpper(), false);
            }

            ShellMethods.SHChangeNotify(Enums.SHCNE.ASSOCCHANGED, 0, IntPtr.Zero, IntPtr.Zero);

            ShellMethods.SHLoadNonloadedIconOverlayIdentifiers();
        }
    }
}
